package com.ifeng.py;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author sujian
 * 提取一个网页中的特征关键词
 * */
public class PyService {
	private Word[] word;
	private int len,Max;
	private String path;
	private int totalWord;
	private int pydep;
	public PyService(){
		Max=50;//长度最大强制存储
		word=new Word[Max];
		len=0;
		this.path="c:\\py.txt";
		this.totalWord=0;
		this.pydep=-1;
		this.checkFolder(this.path);
	}
	public PyService(String path,int dep){
		Max=50;//长度最大强制存储
		word=new Word[Max];
		len=0;
		this.path=path;
		this.pydep=dep;
		this.checkFolder(this.path);
	}
	public int savePyToPath(String host,String url,int pot,String...aClass){
		try {
			this.getMesByUrl(host, url, pot, aClass);
			this.savePy();//把缓存中的数据存入文件
			return this.totalWord;
		} catch (Exception e) {
			// TODO: handle exception
			//do something
			return 0;
		}
	}
	/**
	 * @param url keyClass(标签中的class名)
	 * @return List<String> (网页中,所有的结果)
	 * */
	public List<String> getMesByUrl(String host,String url,int pot,String...aClass) throws Exception{
		List<String> uList=new LinkedList<String>();
		try {
			List<Model> mList=this.getModelByClass(host+url,aClass[pot]);
			Iterator<Model> iters=mList.iterator();
			Model ml;
			int dep=0;
			while(iters.hasNext() && (this.pydep==-1 || dep<this.pydep || pot+1>=aClass.length))
			{
				dep++;
				ml=null;
				ml=iters.next();
				if(pot+1>=aClass.length)
				{
					uList.add(ml.getAtext().substring(0,1));
				}
				else{
					Word oneWord=new Word();
					oneWord.setPy(ml.getAtext());
					oneWord.addAll(this.getMesByUrl(host,ml.getAhref(),pot+1,aClass));
					word[len++]=oneWord;
					System.out.println("已经获取:"+oneWord.getPy());
					if(len>=Max){
						this.savePy();
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
		return uList;
	}
	/**
	 * 抓取指定的class属性下的超链接
	 * */
	protected List<Model> getModelByClass(String url,String aClass) throws Exception{
		List<Model> mList= new LinkedList<Model>();
		try {
			//这种方法可以避免抓取回来的网页出现乱码，可以指定格式
			Document doc=Jsoup.parse(new URL(url).openStream(),"GBK",url);
			Elements links=doc.getElementsByAttributeValue("class",aClass);
			for(Element link : links)
			{
				String lhref=link.attr("href");
				String ltext=link.text();
				Model aModel=new Model();
				aModel.setAhref(lhref);
				aModel.setAtext(ltext);
				mList.add(aModel);
			}
		} catch (Exception e) {
			throw e;
		}
		return mList;
	}
	/**
	 * 文件存在就删除
	 * */
	public boolean checkFolder(String path){
		try {
			File file = new File(path);
			return file.delete();
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	/**
	 * 保存抓取的信息
	 * */
	private boolean savePy(){
		try {
			BufferedWriter bw=new BufferedWriter(
					new OutputStreamWriter(
							new FileOutputStream(path,true),"utf-8"));
			for(int i=0;i<len;i++){
				bw.append(word[i].getPy()+"\n");
				List<String> pList=new ArrayList<String>(new HashSet<String>(word[i].getZh()));
				for(String wpy : pList){
					this.totalWord++;
					bw.append(wpy+" ");
				}
				bw.append("\n");
				word[i]=null;//强制回收
			}
			bw.close();
			len=0;
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
}
